#!/bin/bash

Install_Moodle()
{
    echo -e " \e[0;32m[+] Download Moodle Git Clone ...\e[0m"
    cd ${cur_dir}/src
    if [ -s "${MoodleFileName}" ]; then
        echo "${MoodleFileName} [found]"
     else
        echo "Error: ${MoodleFileName} not found!!!download now..."
        wget -c --progress=bar:force --prefer-family=IPv4 --no-check-certificate ${Download_Mirror}/moodle/${MoodleFileName}
    fi

    echo -e " \e[0;32m[+] Install Moodle Soucre...\e[0m"
    if [ -s "${MoodleFileName}" ]; then
        tar zxf ${MoodleFileName} -C ${Default_Moodle_Dir}/
        cd ${Default_Moodle_Dir}/moodle
        git reset --hard
        git pull
        MoodleVer=$( git branch -a | sed '$d' | sed -n '$p' | awk -F "/" '{print $3}' )
        git branch --track ${MoodleVer} origin/${MoodleVer}
        git checkout ${MoodleVer}
        git pull
        git branch -D master
      else
        echo "Moodle ${Moodle_Version} Not Found!! download now..."
        rm -fr ${Default_Moodle_Dir}/moodle
        git clone https://gitee.com/hongm/imoodle.git ${Default_Moodle_Dir}/moodle
        if [ $? -eq 0 ]; then
            cd ${Default_Moodle_Dir}/moodle
            MoodleVer=$( git branch -a | sed '$d' | sed -n '$p' | awk -F "/" '{print $3}' )
            git branch --track ${MoodleVer} origin/${MoodleVer}
            git checkout ${MoodleVer}
            git pull
            git branch -D master
         else
            n=40
            while [ $n -ge 31 ]
            do
                FileName=moodle-latest-$n.tgz
                wget -cN http://download.moodle.org/download.php/direct/stable$n/${FileName}
                if [ -s ${FileName} ]; then
                    break
                fi
                n=$((n-1))
            done
            tar zxf ${FileName} -C ${Default_Moodle_Dir}/
        fi
    fi
    cd ${cur_dir}/src
    MoodleConfigFile="${Default_Moodle_Dir}/moodle/config.php"
    echo '<?PHP  // Moodle configuration file' > ${MoodleConfigFile}
    echo 'unset($CFG);' >> ${MoodleConfigFile}
    echo 'global $CFG;' >> ${MoodleConfigFile}
    echo '$CFG = new stdClass();' >> ${MoodleConfigFile}
    echo '$CFG->dbtype    = "mariadb";' >> ${MoodleConfigFile}
    echo '$CFG->dblibrary = "native";' >> ${MoodleConfigFile}
    echo '$CFG->dbhost    = "localhost";' >> ${MoodleConfigFile}
    echo '$CFG->dbname    = "moodle";' >> ${MoodleConfigFile}
    echo '$CFG->dbuser    = "moodle";' >> ${MoodleConfigFile}
    echo "\$CFG->dbpass    = \"${DB_Root_Password}\";" >> ${MoodleConfigFile}
    echo '$CFG->prefix    = "mdl_";' >> ${MoodleConfigFile}
    echo '$CFG->dboptions = array(' >> ${MoodleConfigFile}
    echo '  "dbpersist" => 0,' >> ${MoodleConfigFile}
    echo '  "dbport" => "",' >> ${MoodleConfigFile}
    echo '  "dbsocket" => "",' >> ${MoodleConfigFile}
    echo '  "dbcollation" => "utf8mb4_unicode_ci",' >> ${MoodleConfigFile}
    echo ');' >> ${MoodleConfigFile}
    echo "\$CFG->wwwroot   = \"http://${IP}\";" >> ${MoodleConfigFile}
    echo '$CFG->dataroot  = "'${Default_Moodle_Dir}'/moodledata";' >> ${MoodleConfigFile}
    echo '$CFG->directorypermissions = 0750;' >> ${MoodleConfigFile}
    echo '$CFG->admin = "admin";' >> ${MoodleConfigFile}
    echo 'require_once(dirname(__FILE__) . "/lib/setup.php"); // Do not edit' >> ${MoodleConfigFile}
    mkdir -p ${Default_Moodle_Dir}/moodledata
    chown -R www:www ${Default_Moodle_Dir}/moodle
    chown -R www:www ${Default_Moodle_Dir}/moodledata
    chmod -R 0750 ${Default_Moodle_Dir}/moodle
    chmod -R 0750 ${Default_Moodle_Dir}/moodledata
    find ${Default_Moodle_Dir}/moodle -type f -exec chmod 0640 {} \;

    echo -e " \e[0;32m[+] Install Moodle DataBase ...\e[0m"
    echo "grant all on moodle.* to 'moodle'@'localhost' identified by '${DB_Root_Password}';" > /tmp/mariadb_sec_script
    echo "grant all on moodle.* to 'moodle'@'127.0.0.1' identified by '${DB_Root_Password}';" >> /tmp/mariadb_sec_script
    echo "flush privileges;" >> /tmp/mariadb_sec_script
    echo "CREATE DATABASE moodle DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;" >> /tmp/mariadb_sec_script
    /usr/bin/mysql -u root -p${DB_Root_Password} -h localhost < /tmp/mariadb_sec_script
    rm -f /tmp/mariadb_sec_script
    n=0
    while [ $n -le 8 ] && [ ! -f ${MySQL_Data_Dir}/moodle/mdl_user.frm ] && [ ! -f ${DB_Data_Dir}/moodle/mdl_user.frm ]
    do
        sudo -u www php ${Default_Moodle_Dir}/moodle/admin/cli/install_database.php --lang=zh_cn --adminpass=${Admin_Password} --adminemail=${Admin_Email} --fullname=ITCenter --shortname=ITCenter --agree-license
        n=$((n+1))
    done
    if [ $n -ge 8 ]; then
        echo "Network error! Please continue to install http://${IP} in the browser!"
     else
        /usr/bin/mysql -u root -p${DB_Root_Password} -h localhost -e "UPDATE moodle.mdl_config SET value='firstname,lastname' WHERE name='alternativefullnameformat';"
        /usr/bin/mysql -u root -p${DB_Root_Password} -h localhost -e "UPDATE moodle.mdl_config SET value='0' WHERE name='defaulthomepage';"
        /usr/bin/mysql -u root -p${DB_Root_Password} -h localhost -e "UPDATE moodle.mdl_config SET value='CN' WHERE name='country';"
        /usr/bin/mysql -u root -p${DB_Root_Password} -h localhost -e "UPDATE moodle.mdl_config SET value='Asia/Shanghai' WHERE name='timezone';"
        /usr/bin/mysql -u root -p${DB_Root_Password} -h localhost -e "UPDATE moodle.mdl_config SET value='' WHERE name='docroot';"
        cd ${cur_dir}/src
        wget -cN ${Download_Mirror}/moodle/langpack/zh_cn_local.tar.gz
        if  [ -f zh_cn_local.tar.gz ]; then
            tar zxf zh_cn_local.tar.gz -C ${Default_Moodle_Dir}/moodledata/lang
            chown -R www:www ${Default_Moodle_Dir}/moodledata/lang/zh_cn_local
            chmod -R 0750 ${Default_Moodle_Dir}/moodledata/lang/zh_cn_local
            rm -fr zh_cn_local.tar.gz
        fi
    fi
}

